package com.qianfeng.teachdemo.ui;

import java.util.HashMap;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;


import com.lidroid.other.UtilsTextStyle;
import com.lidroid.verification.Rule;
import com.lidroid.verification.Validator;
import com.lidroid.verification.annotation.TextRule;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ContentView;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.lidroid.xutils.view.annotation.event.OnClick;
import com.qianfeng.teachdemo.R;
import com.qianfeng.teachdemo.bean.UserInfo;


/**
 * @version 1.0
 * @author: 张唯
 * @类 说 明:
 * @创建时间：2014年7月6日 下午8:39:41
 */
@ContentView(R.layout.activity_login)
public class LoginActivity extends BaseActivity implements Validator.ValidationListener {
    @ViewInject(R.id.login_head)
    ImageView headIv;
    @ViewInject(R.id.login_username)
    @TextRule(minLength = 4, message = "文字长度错误", order = 8)
    EditText userEt;
    @ViewInject(R.id.login_password)
    @TextRule(minLength = 6, message = "文字长度错误", order = 8)
    EditText pswEt;
    @ViewInject(R.id.loginBtn)
    Button loginBtn;
    @ViewInject(R.id.login_register_account)
    private TextView registerTv;
    @ViewInject(R.id.login_find_psw)
    private TextView findPswTv;
    private UserInfo userInfo;
    private Validator validator;
    private String method = "login";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ViewUtils.inject(this);
    }

    @OnClick({R.id.login_register_account, R.id.loginBtn, R.id.login_find_psw})
    public void OnClickListener(View v) {
        switch (v.getId()) {
            case R.id.login_register_account:
                activity.startActivity(new Intent(activity, RegisterActivity.class));
                break;
            case R.id.loginBtn:
                validator = new Validator(this);
                validator.setValidationListener(this);
                validator.validate();
                break;
            case R.id.login_find_psw:
                break;
        }
    }

    @Override
    public void onValidationSucceeded() {
        HashMap<String, String> params = new HashMap<String, String>();
        params.put("userName", userEt.getText().toString());
        params.put("password", pswEt.getText().toString());
    }

    @Override
    public void onValidationFailed(View failedView, Rule<?> failedRule) {
        String message = failedRule.getFailureMessage();
        if (failedView instanceof EditText) {
            failedView.requestFocus();
            UtilsTextStyle textStyle = new UtilsTextStyle();
            textStyle.setString(message);
            textStyle.setBackgroundColor(Color.WHITE, 0, message.length());
            ((EditText) failedView).setError(textStyle.getSpannableString());
        } else {
        }
    }
}
